<?php

namespace App\Http\Controllers;
$base = base_path() . '/vendor/voiceax/';
include_once($base . 'PrivacyAxBindBody.php');
include_once($base . 'WinnerVoiceSender.php');
include_once($base . 'PrivacyUnbindBoody.php');
include_once($base . 'WinnerVoiceSender.php');

class AxCallController extends BaseController
{

    /** AX模式绑定示例代码 */
    private static function doAxBind($mid_phone, $a_phone, $time = 3600)
    {
        $PRIVACY_AX_URL = '/voice/1.0.0/middleNumberAX';
        $axBody = new \PrivacyAxBindBody();
        /**设置为平台分配的隐私号码*/
        $axBody->middleNumber = $mid_phone;
        /**设置为需要绑定的号码A*/
        $axBody->bindNumberA = $a_phone;
        /**设置为需要呼叫的号码B，可不填*/
//        $axBody->bindNumberB = $b_phone;
        $axBody->calleeName = "马化腾";
        $axBody->calleeId = "654201197909181231";
        /**是否需要录音，需要录音设置为1，不需要录音设置为0*/
        $axBody->callRec = 1;
        /**设置绑定时间，单位秒，当设置为0时，则永久绑定*/
        $axBody->maxBindingTime = $time;
        /**设置话单的推送地址*/
        $axBody->callbackUrl = "";
        /**axn的小号，Mode填1*/
        $axBody->mode = "2";
        $axBody->autoUnbindNumberA = "1";
        /**设置为平台分配的account*/
        $account = config("omnipay.Axcall.appID");
        /**设置为平台分配的token*/
        $token = config("omnipay.Axcall.TOKEN");
        $sender = new \WinnerVoiceSender($account, $token, true);
        $result = $sender->sendRequest($PRIVACY_AX_URL, json_encode($axBody));
        $voice_result = json_decode($result);
        if ($voice_result->result == '000000') {
            return true;
        } else {
            return $voice_result;
        }
    }


    /** AXb模式绑定示例代码 */
    private static function doAxbBind($mid_phone, $a_phone,$b_phone, $time = 3600)
    {
        $PRIVACY_AXB_URL='/voice/1.0.0/middleNumberAXB';
        $axBody = new \PrivacyAxBindBody();
        /**设置为平台分配的隐私号码*/
        $axBody->middleNumber = $mid_phone;
        /**设置为需要绑定的号码A*/
        $axBody->bindNumberA = $a_phone;
        /**设置为需要呼叫的号码B，可不填*/
        $axBody->bindNumberB = $b_phone;
        $axBody->calleeName = "马化腾";
//        $axBody->calleeId = "654201197909181231";
        /**是否需要录音，需要录音设置为1，不需要录音设置为0*/
//        $axBody->callRec = 1;
        /**设置绑定时间，单位秒，当设置为0时，则永久绑定*/
        $axBody->maxBindingTime = $time;
        $axBody->maxCallDuration = $time; //设置自动挂断
        /**设置话单的推送地址*/
//        $axBody->callbackUrl = "";
        /**axn的小号，Mode填1*/
//        $axBody->mode = "3";
//        $axBody->autoUnbindNumberA = "1";
        /**设置为平台分配的account*/
        $account = config("omnipay.Axcall.appID");
        /**设置为平台分配的token*/
        $token = config("omnipay.Axcall.TOKEN");
        $sender = new \WinnerVoiceSender($account, $token, true);
        $result = $sender->sendRequest($PRIVACY_AXB_URL, json_encode($axBody));
        $voice_result = json_decode($result);
        if ($voice_result->result == '000000') {
            return "success";
        } else {
            return $voice_result;
        }
    }

    /** AX|AXB模式解绑示例代码 */
    private static function aXUnbind($middleNumber, $numberA,$numberB="")
    {
        $PRIVACY_UNBIND_URL = '/voice/1.0.0/middleNumberUnbind';
        $unbindBody = new \PrivacyUnbindBoody();
        $unbindBody->middleNumber = $middleNumber;
        $unbindBody->bindNumberA = $numberA;
        $unbindBody->bindNumberB = $numberB;
        /**axn 的小号mode填1*/
        $unbindBody->mode = "2";
        /**设置为平台分配的account*/
        $account = config("omnipay.Axcall.appID");
        /**设置为平台分配的token*/
        $token = config("omnipay.Axcall.TOKEN");
        $sender = new \WinnerVoiceSender($account, $token, true);
        $result = $sender->sendRequest($PRIVACY_UNBIND_URL, json_encode($unbindBody));
        $voice_result = json_decode($result);
        if ($voice_result->result == '000000') {
            return "success";
        } else {
            return $voice_result;
        }
    }

    /**
     * 绑定小号AX
     */
    public function doBindPhone($data)
    {
//        $data['mid_phone'] = "02160592280";
//        $data['a_phone'] = "13552907629";
//        $data['time'] = 600;
        $result = self::doAxBind($data['mid_phone'], $data['a_phone'], $data['time']);
        if ($result === true) {
            return true;
        } else {
            return false;
        }
        dd($result);
    }

    /**
     * @param $data
     * @return bool
     * 绑定axb
     */
    public function doBindAxbPhone($data)
    {
//        $data['mid_phone'] = config("omnipay.Numbers");//    "01086482137";
//        $data['a_phone'] = "15690469916";
//        $data['b_phone'] = "13552907629";
////        $data['a_phone'] = "13261215385";
////        $data['b_phone'] = "15801512319";
//        $data['time'] = 600;
        $result = self::doAxbBind($data['mid_phone'], $data['a_phone'],$data['b_phone'], $data['time']);
        if ($result === "success") {
            return "success";
        } else {
            return false;
        }
        dd($result);
    }



    /**
     * 解绑小号AX
     */
    public function doAxUnbind($data)
    {
//        $data['mid_phone'] = config("omnipay.Numbers");;
//        $data['a_phone'] = "13131690656";
//        $data['b_phone'] = "15690469916";
        $res = self::aXUnbind($data['mid_phone'], $data['a_phone'],$data['b_phone']);
        if ($res === "success") {
            return "success";
        } else {
            return false;
        }
        dd($res);
    }

    /**
     * @param $data
     * @return bool
     * 解绑axb
     */
    public function doAxbUnbind()
    {
        $data['mid_phone'] = config("omnipay.Numbers");
        $data['a_phone'] = "19931319990";
        $res = self::aXUnbind($data['mid_phone'], $data['a_phone']);
        if ($res === 'success') {
            return "success";
        } else {
            return false;
        }
        dd($res);
    }
}
